import Pages from '../../../../pages/pages.vue'
import { showMsg, 
  prizeResultList, 
  setMenuType, 
  getSubscribeCourseList, 
  getSubscribeCourseDuration,
  prizeSubscribeCourse
 } from '../../../../../vuex/action'

export default {
  name: 'result-list',
  data() {
    return {
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      },
      currentList: [],
      checkedIds: [],
      checkAll: false,
      noSelectUser: true,
      currentName: '',
      prizeRList: [],
      pList: '',
      form: {
        ids: '',
        courseId: '',
        dateNum: ''
      },
      courseList: [],
      courseDuration: []
    }
  },
  vuex: {
    getters: {
      activityId: state => state.activityStoreId,
      activityName: state => state.activityStoreName,
      fresh: state => state.classesListNeedFresh
    },
    actions: {
      xPrizeResultList: prizeResultList,
      xShowMsg: showMsg,
      xSetMenuType: setMenuType,
      xGetList: getSubscribeCourseList,
      xGetDuration: getSubscribeCourseDuration,
      xSubscribe: prizeSubscribeCourse
    }
  },
  components: {
    'vue-pages': Pages
  },
  computed: {
    prizeNList() {
      let tmp = []
      for(var i in this.prizeRList) {
        if(tmp.indexOf(this.prizeRList[i].name) == -1){
          tmp.push(this.prizeRList[i].name)
        }
      }
      return tmp
    },
    isTingSui() {
      return this.currentName.indexOf('听谁')> -1
    },
    notSendList(){
      let tmp = []
      for(let it of this.currentList){
        console.log(it)
        if(+it.status === 1){
          tmp.push(it)
        }
      }
      return tmp
    },
    sendList(){
      let tmp = []
      for(let it of this.currentList){
        if(+it.status !== 1){
          tmp.push(it)
        }
      }
      return tmp
    }
  },
  ready() {
    this.xSetMenuType('activity')
    this.getList({})
    this.xGetList({ start: 0, limit: 1000 }).then(ret => {
      for (let k in ret) {
        this.courseList.push({ key: k, value: ret[k] })
      }
    })
  },
  methods: {
    valid() {
      if(this.noSelectUser){
        return Promise.reject({
          error_msg: '至少选择一条中奖记录'
        })
      }
      if(this.isTingSui){
        this.form.courseId = ''
        this.form.dateNum = ''
        return Promise.resolve()
      }
      let fields = ['courseId', 'dateNum']
      for (let k of fields) {
        let v = this.form[k] + ''
        if (v.length === 0) {
          return Promise.reject({
            error_msg: '*标识的均为必填项'
          })
        }
      }
      return Promise.resolve()
    },
    getList({start=0, limit=10000, activityId=this.activityId}) {
      let param = {
        start: start,
        limit: limit,
        activityId: +activityId,
        type: 4
      }
      this.xPrizeResultList(param)
        .then((data) => {
          if(!this.currentName && data.list.length > 0){
            this.currentName = data.list[0].name
          }
          this.prizeRList = data.list
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    getCurrentList({
      start= this.pages.start,
      limit= this.pages.limit,
    }) {
      this.currentList = this.pList.slice(start, start + limit)
    },
    submit() {
      this.valid()
        .then(() => {
          let form = this.form
          this.form.ids = this.checkedIds.join()
          return this.xSubscribe(form)
        })
        .then(() => {
          this.getList({})
          this.checkedIds = []
          this.xShowMsg({
            type: 'info',
            content: '课程发送成功'
          })
        })
        .catch(res => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  },
  watch: {
    'form.courseId': function(val) {
      this.form.dateNum = ''
      if (val) {
        this.xGetDuration({
          courseId: val
        }).then(durationList => {
          this.courseDuration = durationList.map(it => {
            return {
              key: it.duration,
              value: it.duration + '日'
            }
          })
        })
      } else {
        this.courseDuration = []
      }
    },
    checkAll() {
      this.checkedIds = []
      for (var item of this.pList) {
        if (this.checkAll && +item.status === 1) {
          this.checkedIds.push(item.id + '')
        }
      }
    },
    currentName(){
      let tmp  = []
      for(var i in this.prizeRList) {
        if(this.prizeRList[i].name === this.currentName){
          tmp.push(this.prizeRList[i])
        }
      }
      this.pList = tmp
      this.pages.start = 0
      this.getCurrentList({})
      this.pages.totalPages = Math.ceil(this.pList.length / this.pages.limit)
      this.checkedIds = []
    },
    checkedIds() {
      if (this.checkedIds.length === 0) {
        this.noSelectUser = true
      } else {
        this.noSelectUser = false
      }
    },
    prizeRList() {
      let tmp  = []
      for(var i in this.prizeRList) {
        if(this.prizeRList[i].name === this.currentName){
          tmp.push(this.prizeRList[i])
        }
      }
      this.pList = tmp
      this.getCurrentList({})
      this.pages.totalPages = Math.ceil(this.pList.length / this.pages.limit)
    },
    fresh(val) {
      if (val) {
        this.getList({})
      }
    }
  }
}
